﻿
//导出建表sql，by推送组
function exportGroupCreateTableScript(groupID) {
    var url = "/ViewGroup/ExportCreateTableScriptByGroupID?groupID=" + groupID + "&systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]);
    downloadfile(url, null);
}
// 导出建表语句 by 模型
function exportViewCreateTableScript(viewID) {
    var url = "/ViewGroup/ExportCreateTableScriptByViewID?viewID=" + viewID + "&systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]);
    downloadfile(url, null);
}
// 添加模型
function selectModelToGroup(groupID) {
    var systemID = UrlArray["systemID"];
    var url = '/ViewGroup/ModelList?systemID=' + systemID + '&dbName=' + encodeURI(UrlArray["dbName"]);
    showModalMany('SelectTargetList', url, '810', '460',
                function (result) {
                    var viewIDs = new Array();
                    if (result.length > 0) {
                        for (var i = 0; i < result.length; i++) {
                            var value = result[i].Guid;
                            viewIDs.push(value);
                        }
                        addModelsToGroup(groupID, viewIDs);
                    }
                });
}

function addModelsToGroup(groupID, viewIDArray) {
    var systemID = UrlArray["systemID"];
    var dbName = encodeURI(UrlArray["dbName"]);
    var viewIDs = viewIDArray.toString();

    var postUrl = "/ViewGroup/AddModelsToGroup?dbName=" + dbName;
    var postData = { systemID: systemID, groupID: groupID, viewIDs: viewIDs };
    $.ajax({
        type: "POST",
        async: true,
        url: postUrl,
        data: postData,
        success: function (result) {
            if (result.Success == true) {
                alert(result.Message);
            }
            else {
                alert(result.Message);
            }
            InitList();
        }
    });
}
// 推送配置
function openViewModelPushConfig(groupID) {
    var systemID = UrlArray["systemID"];
    var url = '/ViewGroup/ViewModelPushConfig?systemID=' + systemID + "&groupID=" + groupID + '&dbName=' + encodeURI(UrlArray["dbName"]);
    if (UrlArray["DBType"] == "2")
        url += "&DBType=" + UrlArray["DBType"];
    showModalMany('ViewModelPushConfig', url, '810', '460');
    InitList();
}
// 编辑推送名称
function editPushName(viewID) {
    var psName;
    $("#txt_Schema").val("");
    $("#txt_PushName").val("");
    for (var i = 0; i < pushName.length; i++) {
        if (pushName[i].Guid == viewID) {
            $("#txt_Schema").val(pushName[i].PushSchema);
            $("#txt_PushName").val(pushName[i].PushName);
        }
    }
    $('#ViewPushName').show();
    $('#DIVMask').show();
    $("#hid_ViewID").val(viewID);
     
}
//保存推送名称
function SavePushName() {
    var Schema = $.trim($("#txt_Schema").val());
    var pushName = $.trim($("#txt_PushName").val());
    var viewID = $("#hid_ViewID").val();
    var dbName = encodeURI(UrlArray["dbName"]);
    var systemID = UrlArray["systemID"];

    if (pushName == "") {
        alert("请输入推送名称");
        return;
    } 
    var postUrl = "/ViewGroup/UpdatePushName/?dbName=" + encodeURI(UrlArray["dbName"]);
    var postData = { pushName: pushName, schema: Schema, viewID: viewID, systemID: systemID };
    $.ajax({
        type: "POST",
        async: true,
        url: postUrl,
        data: postData,
        success: function (result) {
            if (result.Success == true) {
                alert(result.Message);
            }
            else {
                alert(result.Message);
            }
            $('#ViewPushName').hide(); 
            $('#DIVMask').hide();
            window.location.reload();
        }
    });
}
// 编辑推送组
function EditGroupItem(groupID) {
    var url = "/ViewGroup/Add?groupID=" + groupID + "&systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]);
    if (UrlArray["DBType"] == "2")
        url += "&DBType=" + UrlArray["DBType"];
    window.location.href = url;
}
// 删除推送组
function DeleteGroupItem(groupID) {
    if (confirm("确定要删除吗?")) {
        $.post("/ViewGroup/Delete?groupID=" + groupID + "&systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]), function (result) {
            if (result.Success == true) {
                alert(result.Message);
            }
            else {
                alert(result.Message);
            }
            InitList();
        });
    }
} // Ajax 文件下载 
function downloadfile(url, data) {
    if (url || data) {
        var inputs = '';
        if (typeof data !== "undefined" && data != null) {
            $.each(data, function () {
                var pair = this.split('=');
                inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />';
            });
        }
        $('<form action="' + url + '" method="post">' + inputs + '</form>').appendTo('body').submit().remove();
    };
}
// 配置模型字段
function openColumnConfigPage(vcid) {
    var id = UrlArray["systemID"];
    if (typeof (id) == "undefined" || id == "") {
        id = $("#hddEtlTargetId").val();
    }
    var url = '/EtlTarget/EtlTargetColumnConfig?systemID=' + id + "&viewID=" + vcid + "&dbName=" + encodeURI(UrlArray["dbName"]);
    if (UrlArray["DBType"] == "2")
        url += "&DBType=" + UrlArray["DBType"];
    showModalMany('Contact', url, '810', '480');
    InitList();
}
//取消配置字段
function deleteColumnConfig(viewID, removeGroup) {
    if (confirm("确定要取消配置？")) {
        var systemID = UrlArray["systemID"];
        var dbName = encodeURI(UrlArray["dbName"]);
        var postUrl = "/EtlTarget/DeleteTargetViewColumns?dbName=" + dbName;
        var postData = { systemID: systemID, viewID: viewID };
        $.ajax({
            type: "POST",
            async: true,
            url: postUrl,
            data: postData,
            success: function (result) {
                if (result.Success == true) {
                    if (removeGroup == "")
                        alert(result.Message);
                    else {
                        removeModelFromGroup(viewID, false);
                    }
                }
                else {
                    alert(result.Message);
                }
                InitList();
            }
        });
    }
}
//删除对接模型
function deleteTargetView(viewID) {
    if (confirm("确定要删除吗？")) {
        var systemID = UrlArray["systemID"];
        var dbName = encodeURI(UrlArray["dbName"]);
        var postUrl = "/EtlTarget/DeleteTargetView?dbName=" + dbName;
        var postData = { systemID: systemID, viewID: viewID };
        $.ajax({
            type: "POST",
            async: true,
            url: postUrl,
            data: postData,
            success: function (result) {
                if (result.Success == true) {
                    alert(result.Message);
                }
                else {
                    alert(result.Message);
                }
                InitList();
            }
        });
    }
}
//查看etl
function ViewETLData(id) {
    var dbName = encodeURI(UrlArray["dbName"]);
    var url = "/SystemConfiguration/ViewETLData/?viewID=" + id + "&dbName=" + dbName;
    showModalMany('ViewETLData', url, '810', '430');
    InitList();
}

// 预览模型数据
function openViewModelData(vcid) {
    var id = UrlArray["systemID"];
    if (typeof (id) == "undefined" || id == "") {
        id = $("#hddEtlTargetId").val();
    }
    showModalMany('Contact', '/EtlTarget/ViewModelData?systemID=' + id + "&viewID=" + vcid + "&dbName=" + encodeURI(UrlArray["dbName"]), '800', '470', function (reValue) {
        InitList();
    });
    InitList();
}
// 取消与组关联
function removeModelFromGroup(viewID, ale) {
    var pass = false;
    if (ale) {
        if (confirm("确定要取消与组关联吗？")) {
            pass = true;
        }
    } else
        pass = true;
    if (pass) {
        var dbName = encodeURI(UrlArray["dbName"]);
        var systemID = UrlArray["systemID"];
        var groupID = UrlArray["groupID"];
        var postUrl = "/ViewGroup/RemoveModelFromGroup/?dbName=" + encodeURI(UrlArray["dbName"]);
        var postData = { viewID: viewID, systemID: systemID, groupID: groupID };
        $.ajax({
            type: "POST",
            async: true,
            url: postUrl,
            data: postData,
            success: function (result) {
                if (result.Success == true) {
                    alert(result.Message);
                }
                else {
                    alert(result.Message);
                }
                InitList();
            }
        });
    }
}